package com.bawei.image

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.luck.picture.lib.engine.ImageEngine
import com.luck.picture.lib.utils.ActivityCompatHelper

/**
 * Author: Jason Zhang
 * Date: 2024/8/30
 * Desc:
 */
class GlideEngine: ImageEngine {

    companion object{
        private val instance: GlideEngine = GlideEngine()
        fun createGlideEngine() = instance
    }

    override fun loadImage(context: Context, url: String, imageView: ImageView) {
        if (!ActivityCompatHelper.assertValidRequest(context)){
            return
        }
        Glide.with(context).load(url).into(imageView)
    }

    override fun loadImage(context: Context, imageView: ImageView, url: String, maxWidth: Int, maxHeight: Int) {
        if (!ActivityCompatHelper.assertValidRequest(context)){
            return
        }
        Glide.with(context).load(url).override(maxWidth, maxHeight).into(imageView)
    }

    override fun loadAlbumCover(context: Context, url: String, imageView: ImageView) {
        if (!ActivityCompatHelper.assertValidRequest(context)){
            return
        }
        Glide.with(context)
            .load(url)
            .override(180, 180)
            .transform(CenterCrop(), RoundedCorners(8))
            .placeholder(ColorDrawable(Color.GRAY))
            .into(imageView)
    }

    override fun loadGridImage(context: Context, url: String, imageView: ImageView) {
        if (!ActivityCompatHelper.assertValidRequest(context)){
            return
        }
        Glide.with(context)
            .load(url)
            .override(200, 200)
            .centerCrop()
            .placeholder(ColorDrawable(Color.GRAY))
            .into(imageView)
    }

    override fun pauseRequests(context: Context) {
        if (!ActivityCompatHelper.assertValidRequest(context)){
            return
        }
        Glide.with(context).pauseRequests()

    }

    override fun resumeRequests(context: Context) {
        if (!ActivityCompatHelper.assertValidRequest(context)){
            return
        }
        Glide.with(context).resumeRequests()
    }


}